<?php
namespace app\admin\controller;

use app\common\model\ShopStore;
use app\common\model\User;
use app\shop\model\ShopOrder;
use app\admin\service\Echarts;
use buildView\Grid;
use think\Controller;
use think\Db;
use app\common\model\ShopSchoolRegion;

/**
 * 平台概况
 * Class Platform
 * @package app\admin\controllerd
 */
class Platform extends Controller
{
     /**
     * 平台概况
     * @auth true
     * @menu true
     * @return mixed
     */
    public function index()
    {
        $store = session('store');
        if ($store['id']){
            $store_id = $store['id'];
        }else{
            $store_id = $this->request->param('store_id');
            $region_user = session('admin_user');
        }
        $echart = new Echarts();
        //判断超级管理员和普通店家的数据概况显示
        if($store['systemuser']['authorize'] == null){
            if($region_user['region_id'] > 0 and $region_user['region_id'] != null){
                $region_type = ShopSchoolRegion::where('id',$region_user['region_id'])->find();
                if($region_type['pid'] == 0){
                    // 市级管理员
                    $echart->create('shop_order', '账单统计', '', 'line')->setHeader('总营业额', 'sum', 'total_price', function ($q) {
                        $region_user = session('admin_user');
                        $q->where('status', 'neq',-1)->where('is_pay',1)->where('city_id',$region_user['region_id']);
                    })
                    ->sum('预约消费', 'goods_price', function ($q) {
                        $region_user = session('admin_user');
                        $q->where('type', 1)->where('status', 'neq',-1)->where('is_pay',1)->where('city_id',$region_user['region_id']);
                    })
                    ->sum("会员充值", 'goods_price', function ($q) {
                        $region_user = session('admin_user');
                        $q->where('type', 5)->where('status', 'neq',-1)->where('is_pay',1)->where('city_id',$region_user['region_id']);
                    })
                    ->sum('会员卡购买', 'goods_price', function ($q) {
                        $region_user = session('admin_user');
                        $q->where('type', 6)->where('status', 'neq',-1)->where('is_pay',1)->where('city_id',$region_user['region_id']);
                    });
                    $echart->create('user', '会员统计', '', 'line', 'create_at')->setHeader("会员", 'count', '*',function ($q)  {
                        $region_user = session('admin_user');
                        $schoolRegion = ShopSchoolRegion::where('pid',$region_user['region_id'])->select();
                        $region_ids = [];
                        foreach ($schoolRegion as $key=>$value){
                            $region_ids[$key] = $value['id'];
                        }
                        $shopStoreRegion = ShopStore::whereIn('region_id',$region_ids)->select();
                        $store_ids =[];
                        foreach ($shopStoreRegion as $key=>$value){
                            $store_ids[$key] = $value['id'];
                        }
                        $q->whereIn('store_id',$store_ids);
                    })->count("<a href='#/admin/member/index.html'>会员</a>");
                }elseif ($region_type['pid'] > 0) {
                    // 区级管理员
                    $echart->create('shop_order', '账单统计', '', 'line')->setHeader('总营业额', 'sum', 'total_price', function ($q) {
                        $region_user = session('admin_user');
                        $q->where('status', 'neq',-1)->where('is_pay',1)->where('area_id',$region_user['region_id']);
                    })
                    ->sum('预约消费', 'goods_price', function ($q) {
                        $region_user = session('admin_user');
                        $q->where('type', 1)->where('status', 'neq',-1)->where('is_pay',1)->where('area_id',$region_user['region_id']);
                    })
                    ->sum("会员充值", 'goods_price', function ($q) {
                        $region_user = session('admin_user');
                        $q->where('type', 5)->where('status', 'neq',-1)->where('is_pay',1)->where('area_id',$region_user['region_id']);
                    })
                    ->sum('会员卡购买', 'goods_price', function ($q) {
                        $region_user = session('admin_user');
                        $q->where('type', 6)->where('status', 'neq',-1)->where('is_pay',1)->where('area_id',$region_user['region_id']);
                    });
                    $echart->create('user', '会员统计', '', 'line', 'create_at')->setHeader("会员", 'count', '*',function ($q)  {
                        $region_user = session('admin_user');
                        $shopStoreRegion = ShopStore::whereIn('region_id',$region_user['region_id'])->select();
                        $store_ids =[];
                        foreach ($shopStoreRegion as $key=>$value){
                            $store_ids[$key] = $value['id'];
                        }
                        $q->whereIn('store_id',$store_ids);
                    })->count("<a href='#/admin/member/index.html'>会员</a>");
                }
                
            }else{
                // 超级管理员
                $echart->create('shop_order', '账单统计', '', 'line')
                ->setHeader('总营业额', 'sum', 'goods_price', function ($q)use($store_id) {
                    if ($store_id){
                        $q->where('status', 'neq',-1)->where('is_pay',1)->where('store_id',$store_id);
                    }else{
                        $q->where('status', 'neq',-1)->where('is_pay',1);
                    }
                })
                ->sum('预约消费', 'goods_price', function ($q)use($store_id) {
                    if ($store_id) {
                        $q->where('type', 1)->where('status', 'neq',-1)->where('is_pay',1)->where('store_id',$store_id);
                    }else{
                        $q->where('type', 1)->where('status', 'neq',-1)->where('is_pay',1);
                    }
                })
                ->sum("会员充值", 'goods_price', function ($q)use($store_id) {
                    if ($store_id) {
                        $q->where('type', 5)->where('status', 'neq',-1)->where('is_pay',1)->where('store_id',$store_id);
                    }else{
                        $q->where('type', 5)->where('status', 'neq',-1)->where('is_pay',1);
                    }
                })
                ->sum('会员卡购买', 'goods_price', function ($q)use($store_id) {
                    if ($store_id) {
                        $q->where('type', 6)->where('status', 'neq',-1)->where('is_pay',1)->where('store_id',$store_id);
                    }else{
                        $q->where('type', 6)->where('status', 'neq',-1)->where('is_pay',1);
                    }
                });
                $echart->create('user', '会员统计', '', 'line', 'create_at')
                ->setHeader("会员", 'count', '*',function ($q) use ($store_id) {
                    if ($store_id){
                        $q->where('store_id',$store_id);
                    }
                })->count("会员");
            }
        }else{
            //门店管理员
            $echart->create('shop_order', '账单统计', '', 'line')
            ->setHeader('总营业额', 'sum', 'goods_price', function ($q)use($store_id) {
                if ($store_id){
                    $q->where('status', 'neq',-1)->where('is_pay',1)->where('store_id',$store_id);
                }else{
                    $q->where('status', 'neq',-1)->where('is_pay',1);
                }
            })
            ->sum('预约消费', 'goods_price', function ($q)use($store_id) {
                if ($store_id) {
                    $q->where('type', 1)->where('status', 'neq',-1)->where('is_pay',1)->where('store_id',$store_id);
                }else{
                    $q->where('type', 1)->where('status', 'neq',-1)->where('is_pay',1);
                }
            })
            ->sum('会员卡购买', 'goods_price', function ($q)use($store_id) {
                    if ($store_id) {
                        $q->where('type', 6)->where('status', 'neq',-1)->where('store_id',$store_id);
                    }else{
                        $q->where('type', 6)->where('status', 'neq',-1)->where('is_pay',1);
                    }
                });
            $echart->create('user', '会员统计', '', 'line', 'create_at')
            ->setHeader("会员", 'count', '*',function ($q) use ($store_id) {
                if ($store_id){
                    $q->where('store_id',$store_id);
                }
            })->count("会员");
        }
        
        return $echart->view();
    }

    public function store(){
        $region_user = session('admin_user');
        if($region_user['region_id'] > 0 and $region_user['region_id'] != null){
            // 区域代理
            $region_data = [];
            $region_type = ShopSchoolRegion::where('id',$region_user['region_id'])->find();
            if($region_type['pid'] == 0){
                // 市代理
                $schoolRegion = ShopSchoolRegion::where('pid',$region_user['region_id'])->select();
                $region_ids = [];
                foreach ($schoolRegion as $key=>$value){
                    $region_ids[$key] = $value['id'];
                }
                $region_data = $region_ids;   
            }else{
                // 区代理
                $region_data = $region_user['region_id'];
            }
            $data = ShopStore::where('status',1)->whereIn('region_id',$region_data)->withSum(['order' => function ($q) {
            $q->where('status','neq','-1')->where('is_pay',1);
            return 'order_total_price';
            }], 'total_price')->withCount(['user'])->withSum(['order' => function ($q) {
                $q->where('status','neq',-1)->where('type',1)->where('is_pay',1);
                return 'xiyi_total_price';
            }], 'total_price')->withSum(['order' => function ($q) {
                $q->where('status','neq',-1)->where('type',5)->where('is_pay',1);
                return 'cz_total_price';
            }], 'total_price')->append(['site_url','full_address'])->removeOption('order')->order('order_total_price','desc')->select();
            return json(['code' => 1,'data' => $data]);
        }else{
            // halt('不是代理');
            $data = ShopStore::where('status',1)->withSum(['order' => function ($q) {
            $q->where('status','neq','-1')->where('is_pay',1);
            return 'order_total_price';
            }], 'total_price')->withCount(['user'])->withSum(['order' => function ($q) {
                $q->where('status','neq',-1)->where('type',1)->where('is_pay',1);
                return 'xiyi_total_price';
            }], 'total_price')->withSum(['order' => function ($q) {
                $q->where('status','neq',-1)->where('type',5)->where('is_pay',1);
                return 'cz_total_price';
            }], 'total_price')->append(['site_url','full_address'])->removeOption('order')->order('order_total_price','desc')->select();
            return json(['code' => 1,'data' => $data]);
        }
    }
}